package javaCode.chapter16.method;

/**
 * @author 徐州城
 * @version 1.0
 */
public class ThreadMethod01 {
    public static void main(String[] args) throws InterruptedException {

        T t = new T();
        t.setName("徐州城");
        t.setPriority(Thread.MIN_PRIORITY);
        t.start();//启动子线程

        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);//休眠1s
            System.out.println("hi " + i);
        }
        System.out.println(t.getName() + " 线程的优先级=" + t.getPriority());//1

        t.interrupt();//当执行到这里，就会中断 t线程的休眠

    }
}

class T extends Thread{

    @Override
    public void run() {

        while (true) {
            for (int i = 0; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + " 吃包子~~~~" + i);
            }

            try {
                System.out.println(Thread.currentThread().getName() + " 休眠中~~~");
                Thread.sleep(20000);//20s
            } catch (InterruptedException e) {
                //当线程执行到一个interrupt 方法时，就会catch 一个异常，可以加入自己的业务代码
                //InterruptedException 是捕获了一个中断异常.
                System.out.println(Thread.currentThread().getName() + " 被 interrupt了");
            }
        }
    }
}
